package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.AgentInfoMapper;
import cc.rengu.igas.bomp.common.dao.ManagerInfoMapper;
import cc.rengu.igas.bomp.common.dao.OrganizationMapper;
import cc.rengu.igas.bomp.common.dao.impl.AgentInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.ManagerInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.OrganizationMapperImpl;
import cc.rengu.igas.bomp.common.entity.AgentInfo;
import cc.rengu.igas.bomp.common.entity.ManagerInfo;
import cc.rengu.igas.bomp.common.entity.Organization;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.base.Header;
import cc.rengu.igas.bomp.facade.bean.ManagerInfoBean;
import cc.rengu.igas.bomp.facade.request.ManagerInfoListQueryRequest;
import cc.rengu.igas.bomp.facade.response.ManagerInfoListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhangxuran
 * @Date: 2020/4/4 22:10
 * @Description: 拓展经理列表查询
 */
public class ManagerInfoListQueryService extends ManageService {


    private static final String SIGN_TYPE_BANK = "BANK";

    private static final String SIGN_TYPE_AGENT = "AGENT";

    @Override
    protected Object bizBeforeCust() throws Exception {
        ManagerInfoListQueryRequest managerInfoListQueryRequest = new ManagerInfoListQueryRequest();
        ConvertUtil.convertOutput(managerInfoListQueryRequest);
        if (managerInfoListQueryRequest.getPageIndex() < 0 || managerInfoListQueryRequest.getPageSize() < 0) {
            if (managerInfoListQueryRequest.getPageIndex() != -1 && managerInfoListQueryRequest.getPageSize() != -1) {
                rglog.error("拓展经理列表查询，页数页码不符合规范！");
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        if (managerInfoListQueryRequest.getPageSize() > 100) {
            rglog.error("拓展经理列表查询，查询页数超过100条不符合规范！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        return managerInfoListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ManagerInfoListQueryRequest queryRequest = (ManagerInfoListQueryRequest) request;
        Header header = queryRequest.getHeader();
        BizResponse<ManagerInfoListQueryResponse> bizResponse = new BizResponse<>();
        ManagerInfoListQueryResponse queryResponse = new ManagerInfoListQueryResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);

        String signInstName;
        if (SIGN_TYPE_BANK.equals(queryRequest.getSignInstType())) {
            OrganizationMapper organizationMapper = new OrganizationMapperImpl();
            Organization organization = organizationMapper.selectOrganizationByPrimaryKey(header.getInstId(), queryRequest.getSignInstId());
            if (null == organization) {
                rglog.error("拓展经理列表查询处理失败，查询机构表信息为空！instId:{}, signInstId:{},", header.getInstId(), queryRequest.getSignInstId());
                throw new BizException(RespCodeEnum.QUERY_ORGANIZATION_ERROR.getRespCode(), RespCodeEnum.QUERY_ORGANIZATION_ERROR.getRespDesc());
            }
            signInstName = organization.getOrgName();
        } else if (SIGN_TYPE_AGENT.equals(queryRequest.getSignInstType())) {
            AgentInfoMapper agentInfoMapper = new AgentInfoMapperImpl();
            AgentInfo agentInfo = agentInfoMapper.selectAgentInfoByPrimaryKey(header.getInstId(), queryRequest.getSignInstId());
            if (null == agentInfo) {
                rglog.error("拓展经理列表查询处理失败，查询代理机构信息为空！instId:{}, signInstId:{},", header.getInstId(), queryRequest.getSignInstId());
                throw new BizException(RespCodeEnum.QUERY_ORGANIZATION_ERROR.getRespCode(), RespCodeEnum.QUERY_ORGANIZATION_ERROR.getRespDesc());
            }
            signInstName = agentInfo.getAgentName();
        } else {
            rglog.error("拓展经理列表查询处理失败，签约机构类型非法！instId:{}, signInstId:{},signInstType:{}", header.getInstId(), queryRequest.getSignInstId(), queryRequest.getSignInstType());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        int pageIndex = queryRequest.getPageIndex();
        int pageSize = queryRequest.getPageSize();
        List<ManagerInfo> managerInfoList = new ArrayList<>();
        if (-1 == pageIndex || -1 == pageSize) {
            ManagerInfoMapper managerInfoMapper = new ManagerInfoMapperImpl();
            managerInfoList = managerInfoMapper.selectManagerInfoListByCondition(header.getInstId(), queryRequest.getSignInstId(),
                    queryRequest.getSignInstType(), queryRequest.getMangerId(), queryRequest.getMangerName(), queryRequest.getMangerPhone());

            if (CollectionUtils.isEmpty(managerInfoList)) {
                rglog.info("拓展经理列表查询处理失败!");
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            }

        } else {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageSize(pageSize);
            pageInfo.setPageIndex(pageIndex);

            ManagerInfoMapper managerInfoMapper = new ManagerInfoMapperImpl();
            PageResult pageResult = managerInfoMapper.selectManagerInfoPageByCondition(header.getInstId(), queryRequest.getSignInstId(),
                    queryRequest.getSignInstType(), queryRequest.getMangerId(), queryRequest.getMangerName(), queryRequest.getMangerPhone(), queryRequest.getRoleType(), pageInfo);

            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                rglog.info("拓展经理列表查询处理失败!");
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            }

            /* 根据取出记录条数做对应处理 */
            if (pageResult.getResult().isEmpty()) {
                /* 按照接口转换并响应给请求方 */
                rglog.info("根据查询条件查询到0条拓展经理列表信息!");
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            } else {
                List<Object> detailList = pageResult.getResult();
                for (Object objTemp : detailList) {
                    managerInfoList.add((ManagerInfo) objTemp);
                }

                queryResponse.setTotalRows(pageInfo.getTotalRows());
                queryResponse.setTotalRows(pageInfo.getTotalRows());
            }
        }


        if (!CollectionUtils.isEmpty(managerInfoList)) {
            /* 根据分页查询条件截取相关数据记录 */
            List<ManagerInfoBean> managerInfoBeanList = new ArrayList<>();
            managerInfoList.forEach(item -> convertManagerInfoBean(item, managerInfoBeanList, signInstName));
            queryResponse.setManagerInfoList(managerInfoBeanList);
        } else {
            queryResponse.setTotalRows(0);
        }

        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ManagerInfoListQueryResponse managerInfoListQueryResponse = (ManagerInfoListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(managerInfoListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param managerInfo         数据库表结构对象
     * @param managerInfoBeanList 接口对象
     */
    private void convertManagerInfoBean(ManagerInfo managerInfo, List<ManagerInfoBean> managerInfoBeanList, String signInstName) {
        if ("00".equals(managerInfo.getUserStatus())) {
            //只展示正常的数据   00-正常  01-注销
            ManagerInfoBean managerInfoBean = new ManagerInfoBean();
            BeanUtil.beanCopy(managerInfo, managerInfoBean);
            managerInfoBean.setSignInstName(signInstName);
            managerInfoBean.setManagerId(managerInfo.getManagerId());
            managerInfoBean.setManagerName(managerInfo.getManagerName());
            managerInfoBean.setManagerPhone(managerInfo.getManagerPhone());
            managerInfoBeanList.add(managerInfoBean);
        }
    }
}
